package com.zhou.org.controller;


import com.zhou.framework.aop.anotation.LogOption;
import com.zhou.framework.aop.enums.LogLevel;
import com.zhou.framework.dto.IntegersDto;
import com.zhou.framework.model.RS;
import com.zhou.framework.util.PageUtil;
import com.zhou.org.dao.Right;
import com.zhou.org.dto.*;
import com.zhou.org.model.RightSaveBean;
import com.zhou.org.service.RightService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lang.zhou
 * @since 2022-08-23
 */
@Api(tags = "权限接口")
@RestController
@RequestMapping("/sys/right")
public class RightController {
    @Autowired
    private RightService rightService;

    @ApiOperation("查询所有权限key")
    @LogOption(LogLevel.QUERY)
    @GetMapping("/allRightKey")
    public RS allRightKey(){
        return RS.data(rightService.getDistinctRightKey());
    }

    @ApiOperation("清空自身权限")
    @LogOption(value = LogLevel.GRANT,response = true)
    @PostMapping("/clearSelfRight")
    public RS clearSelfRight(@RequestBody @Validated List<RightClearSelfDto> dtoList){
        rightService.clearSelfRight(dtoList);
        return RS.success();
    }

    @PostMapping("/list")
    @LogOption(LogLevel.QUERY)
    @ApiOperation("权限管理列表")
    public Object getPageList(@RequestBody RightQueryDto dto){
        PageUtil.offsetPage(dto);
        List<RightPageDto> right = rightService.getRight(dto);
        return PageUtil.result(dto,right);
    }

    @DeleteMapping("/delete")
    @LogOption(value = LogLevel.GRANT,response = true)
    @ApiOperation("删除权限")
    public Object deleteRights(@RequestBody @Validated IntegersDto dto){
        rightService.deleteRights(dto.getIds());
        return RS.success();
    }

    @ApiOperation("修改权限")
    @LogOption(value = LogLevel.GRANT,response = true)
    @PostMapping("/saveRight")
    public RS save(@RequestBody @Validated RightSaveDto dto){
        Right right = new Right();
        BeanUtils.copyProperties(dto,right);
        rightService.saveRights(right,dto.getOrgIds());
        return RS.success();
    }

    @ApiOperation("修改权限")
    @LogOption(value = LogLevel.GRANT,response = true)
    @PostMapping("/saveTreeRight")
    public RS save(@RequestBody @Validated List<MenuRightSaveDto> dtoList){
        List<RightSaveBean> saveList = new ArrayList<>(dtoList.size());
        for (MenuRightSaveDto dto : dtoList) {
            List<MenuRightSaveDto.Check> nodeList = dto.getChangeNode();
            if(nodeList.size() > 0){
                List<Right> rights = rightService.getRightBy(dto.getRightKey(),dto.getRightType(),dto.getOrgId());
                Map<String,Right> tmp = new HashMap<>(2);
                List<Right> add = new ArrayList<>();
                List<Right> update = new ArrayList<>();
                for(Right right : rights){
                    //获得所有自有权限
                    if(dto.getOrgId().equals(right.getOrgId())){
                        tmp.put(right.getObjectKey(),right);
                    }
                }
                for(MenuRightSaveDto.Check node : nodeList){
                    String objectKey = String.valueOf(node.getId());
                    int clickNum = node.getClickNum();
                    int reject = Objects.equals(clickNum,2) ? 1 : 0;
                    Right right = tmp.get(objectKey);
                    if(right != null){
                        if(right.getReject() != reject){
                            right.setReject(reject);
                            update.add(right);
                        }
                    }else{
                        Right r = new Right();
                        r.setRightKey(dto.getRightKey());
                        r.setRightType(dto.getRightType());
                        r.setObjectKey(objectKey);
                        r.setOrgId(dto.getOrgId());
                        r.setReject(reject);
                        add.add(r);
                    }
                }
                RightSaveBean bean = new RightSaveBean();
                bean.setOrgId(dto.getOrgId());
                bean.setRightKey(dto.getRightKey());
                bean.setRightType(dto.getRightType());
                bean.setAdd(add);
                bean.setUpdate(update);
                saveList.add(bean);
            }
        }

        rightService.saveRight(saveList);
        return RS.success();
    }
}

